<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Post\GoodsBatchRestore;
use App\Admin\Actions\Post\GoodsRestore;
use App\Admin\Repositories\GrUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Actions\Post\Restore;
use App\Admin\Actions\Post\BatchRestore;
use Illuminate\Support\Facades\Hash;

class GrUserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GrUser(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('head_portrait')->image('',100,100);
            $grid->column('username');
            $grid->column('sex');
            $grid->column('tel');
            $grid->column('level');
            $grid->column('balance');
            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

                // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
                $filter->scope('trashed', '回收站')->onlyTrashed();
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (request('_scope_') == 'trashed') {
                    $actions->append(new GoodsRestore(\App\Models\GrUser::class));
                }
            });
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                if (request('_scope_') == 'trashed') {
                    $batch->add(new GoodsBatchRestore(\App\Models\GrUser::class));
                }
            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GrUser(), function (Show $show) {
            $show->field('id');
            $show->field('password');
            $show->field('username');
            $show->field('head_portrait');
            $show->field('sex');
            $show->field('payment_code');
            $show->field('real_name');
            $show->field('id_card');
            $show->field('tel');
            $show->field('id_card_start');
            $show->field('id_card_end');
            $show->field('balance');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GrUser(), function (Form $form) {
            $form->display('id');
            $form->text('username');
            $form->password('password');
            // 设置错误信息
            $form->password('password','确认密码')->same('password', '两次密码输入不一致');
            $form->saving(function (Form $form) {
                if ($form->password && $form->model()->password != $form->password) {
                    $form->password = Hash::make($form->password);
//                    $form->password = bcrypt($form->password);
                }
            });
            $form->image('head_portrait')->autoUpload()->saveFullUrl()->uniqueName();
            $form->radio('sex')->options(['男'=>'男','女'=>'女']);
            $form->text('payment_code');
            $form->text('level');
            $form->text('real_name');
            $form->text('id_card');
            $form->text('tel');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
